package com.jiuwu.note.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.jiuwu.note.po.ShopersPO;

@Repository
public class ShopersDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<ShopersPO> getAllUser() {
		String sql = "SELECT T._id,T.shoper_name FROM t_shopers AS T;";
		final List<ShopersPO> list = new ArrayList<>();
		RowMapper<ShopersPO> rowMapper = new RowMapper<ShopersPO>() {

			@Override
			public ShopersPO mapRow(ResultSet rs, int rowNum) throws SQLException {
				ShopersPO po = new ShopersPO();
				po.setId(rs.getInt("_id"));
				po.setShoperName(rs.getString("shoper_name"));
				list.add(po);
				return po;
			}
		};

		jdbcTemplate.query(sql, new Object[] {}, rowMapper);
		return list;
	}

	public int addGoods(ShopersPO po) {

		// String sql = "INSERT INTO t_hot_goods (goods_name,maning_price,add_time)
		// VALUES ( '爱他美【3段】奶粉' , 218 , now() );";
		String sql = "INSERT INTO t_shopers (shoper_name,shoper_address,shoper_tel,add_time) VALUES ( ? , ? , ? , ? );";
		return jdbcTemplate.update(sql,
				new Object[] { po.getShoperName(), po.getShoperAddress(), po.getShoperTel(), new Date() });
	}

}
